<template>
  <el-card>
    <div slot="header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">商品列表</a></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 筛选区域 -->
    <div class="filter-bar">
      <div class="l-form">
        <el-input
          placeholder="请输入内容"
          size="small"
          v-model="query"
          clearable
          @clear="resetquery"
        >
          <template>
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchquery"
            ></el-button>
          </template>
        </el-input>
      </div>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="small"
        @click="$router.push('goods/add')"
        >添加</el-button
      >
    </div>

    <!-- 表格区域 -->
    <div class="tab-list">
      <el-table :data="goodList" border stripe>
        <el-table-column
          prop="goods_id"
          label="商品 ID"
          width="80px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="400px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="价格"
          width="80px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="goods_number"
          label="数量"
          width="80px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="goods_state"
          label="商品状态"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.goods_state === 0"
              >未通过</el-tag
            >
            <el-tag type="success" v-if="scope.row.goods_state === 1"
              >审核中</el-tag
            >
            <el-tag type="primary" v-if="scope.row.goods_state === 2"
              >已审核</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="hot_mumber"
          label="热销品数量"
          width="100px"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.hot_mumber">是</el-tag>
            <el-tag type="success" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="add_time" label="添加时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.add_time | formateTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template>
            <el-button
              type="success"
              size="small"
              circle
              icon="el-icon-edit"
            ></el-button>
            <el-button
              type="danger"
              size="small"
              circle
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <el-pagination
      background
      @size-change="sizeChange"
      @current-change="pageChange"
      :current-page="pagenum"
      :page-sizes="[10, 20, 30]"
      :page-size="pagesize"
      layout="total,sizes,prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      query: "",
      goodList: [],
      pagenum: 1,
      pagesize: 10,
      total: 0
    };
  },
  created() {
    this.getGoodList();
  },
  methods: {
    async getGoodList() {
      let { data: res } = await this.$axios.get("goods", {
        params: {
          query: this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      console.log(res);
      this.goodList = res.data.goods;
      this.total = res.data.total;
    },
    // 重置
    resetquery() {
      this.query = "";
      this.pagenum = 1;
      this.getGoodList();
    },
    searchquery() {
      this.pagenum = 1;
      this.getGoodList();
    },
    sizeChange(val) {
      this.pagesize = val;
      this.getGoodList();
    },
    pageChange(val) {
      this.pagenum = val;
      this.getGoodList();
    }
  }
};
</script>

<style lang="less" scoped>
</style>